﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALBrands : DALBase
    {
        public static DALBrands Singleton = new DALBrands();

        public GTBrand Add(GTBrand gtBrand)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newBrand = new Brand();
                newBrand.Name = gtBrand.Name;
                newBrand.Type = gtBrand.Type;
                newBrand.Description = gtBrand.Description;
                newBrand.Active = true;
                newBrand.Stamp = DateTime.Now;
                newBrand.LogoPath = gtBrand.LogoPath;
                newBrand.SmallLogoPath = gtBrand.SmallLogoPath;


                db.Brands.InsertOnSubmit(newBrand);
                db.SubmitChanges();
                gtBrand.Id = newBrand.Id;
                return gtBrand;
            }
        }

        public GTBrand Update(GTBrand gtBrand)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brand = db.Brands.SingleOrDefault(x => x.Id == gtBrand.Id);
                brand.Name = gtBrand.Name;
                brand.Type = gtBrand.Type;
                brand.Description = gtBrand.Description;
                brand.Active = gtBrand.Active;
                brand.Stamp = DateTime.Now;
                brand.LogoPath = gtBrand.LogoPath;
                brand.SmallLogoPath = gtBrand.SmallLogoPath;

                db.SubmitChanges();
                return gtBrand;
            }
        }


        public GTBrand Get(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.Brands.SingleOrDefault(x => x.Id == id);
                GTBrand gtBrand = new GTBrand();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }

        public List<GTBrand> GetPaged(int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Brands
                 orderby p.Name
                 select p;


                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTBrand>();
                foreach (var brand in brands.Page(page, count))
                {
                    GTBrand gtBrand = new GTBrand();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }


        public List<GTBrand> GetPaged(BrandTypeEnum brandTypeEnum, int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.Brands
                 where p.Type == (decimal)brandTypeEnum
                 orderby p.Name
                 select p;


                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTBrand>();
                foreach (var brand in brands.Page(page, count))
                {
                    GTBrand gtBrand = new GTBrand();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }


        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                Brand singleOrDefault = db.Brands.SingleOrDefault(x => x.Id == id);

                if (singleOrDefault != null)
                {
                    db.Brands.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }


        public List<GTBrand> GetByType(BrandTypeEnum brandType, int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brds =
                 from p in db.Brands
                 where p.Type == (decimal)brandType
                 orderby p.Name
                 select p;

                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTBrand>();
                foreach (var brand in brands.Page(page, count))
                {
                    GTBrand gtBrand = new GTBrand();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }


    }
}
